using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
using System.CodeDom.Compiler;
using System.CodeDom;
using Microsoft.CSharp;
using System.Data.SqlClient;
using System.Reflection;
using ESBSimpleSamples.ServiceClient.Generator.WSDL;
using ESBSimpleSamples.ServiceClient.Generator.Helpers;


namespace ESBSimpleSamples.ServiceClient.Generator.Scripts
{
    /// <summary>
    /// Generates a DTO class for a service request
    /// </summary>
    public class RequestEntityScript : CodeDomScript
    {
        public override string Name
        {
            get { return "Request Entity"; }
        }

        protected override void AddConstructor()
        {
            CodeConstructor constructor = new CodeConstructor();
            constructor.Attributes = MemberAttributes.Public;
            this.AddMember(constructor);
        }

        protected override void AddLocalFields()
        {
            foreach (WSDLParameter parameter in this._method.Parameters)
            {
                this.AddMember(CodeDomHelper.GetField(CodeDomHelper.GetFieldName(parameter.ParameterInfo.Name), parameter.ParameterInfo.ParameterType));
            }
        }

        protected override void AddProperties()
        {
            foreach (WSDLParameter parameter in this._method.Parameters)
            {
                this.AddMember(CodeDomHelper.GetProperty(parameter.ParameterInfo.Name, CodeDomHelper.GetFieldName(parameter.ParameterInfo.Name), parameter.ParameterInfo.ParameterType));
            }
        }

        protected override void AddMethods()
        {
            //do nothing
        }

        protected override void AddStructs()
        {
            //do nothing
        }

        protected override void Process()
        {

                this.ClassName = string.Format("{0}Request", this._method.RequestTypeName);
                if (string.IsNullOrEmpty(this.Namespace))
                {
                    string[] namespaceParts = this._metadata.TargetNamespace.Replace(@"//", @"/").Split('/');
                    this.Namespace = namespaceParts[1];
                }
                this.OutputClass();
            
        }

        protected override void SetupDeclaration()
        {
            base.SetupDeclaration();
            this.AddImport("System.Xml.Serialization");
            this.Declaration.CustomAttributes.Add(new CodeAttributeDeclaration("Serializable"));
            List<CodeAttributeArgument> xmlArgs = new List<CodeAttributeArgument>();
            xmlArgs.Add(new CodeAttributeArgument("ElementName", new CodePrimitiveExpression(this._method.RequestTypeName)));
            xmlArgs.Add(new CodeAttributeArgument("Namespace", new CodePrimitiveExpression(this._metadata.TargetNamespace)));
            this.Declaration.CustomAttributes.Add(new CodeAttributeDeclaration("XmlRoot", xmlArgs.ToArray()));
        }
    }
}